# -*- coding: utf-8  -*-
#
# 把 ip 和 端口  组合成 Json 发送到本地MQTT服务器 
# sudo pip install paho-mqtt

from get_ip_function import *
import paho.mqtt.client as mqtt
import json
from time import sleep, time

HOST = "211.81.50.157"
PORT = 1883

client = mqtt.Client()
client.username_pw_set("ubuntu-pc", "")   #username: marshal | password: 
client.connect(HOST, PORT, 60)
client.loop_start()  #client1 runs a thread at background


def senf_IP_info():
    iplist = find_all_ip(system)
    hostname = socket.gethostname()
    ip_string = ''
    for ip in iplist:
        # print ip
        # print(type(ip))
        ip_string += ip + '  '
    print ip_string       
    send_data = {"IP ": ip_string, "nmae ": hostname}
    send_json_data = json.dumps(send_data)
    client.publish("/ifconfig", send_json_data)

t1 = time()

while True:
    t2 = time()
    if t2-t1 >= 60:
        senf_IP_info()
        t1 = t2
        print "send ip and name"
  
client.loop_stop()